<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 商品接口
 */
class Goods extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Goods();
    }

    /**
     * 商品列表
     * @param int $company_id 公司id
     */
    public function index(){

//        $userinfo = $this->auth->getUserinfo();
//        $company = \app\admin\model\Company::get($userinfo['company_id']);
        $company_id = $this->request->param('company_id');
        $company = \app\admin\model\Company::get($company_id);
        $this->limitFields = 'id,name,price,images,weith,createtime';
//        $user = $this->auth->getUserinfo();
        list($where, $sort, $order, $offset, $limit , $field) = $this->buildparams();

        $total = $this->model
            ->where(function ($query) use ($company_id){
                $query->where('company_id',$company_id);
            })
            ->where($where)
            ->count();

        $list = $this->model
            ->where(function ($query) use ($company_id){
                $query->where('company_id',$company_id);
            })
            ->field($field)
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
        $result = array("total" => $total, "rows" => $list,'goods_showtype' => $company['goods_showtype']);
        $this->success('',$result);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post();
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $user = $this->auth->getUserinfo();
                    $params['company_id'] = $user['company_id'];
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

    }





}